<template>
  <div>
    <div class="top-panel__box">
      <el-form :model="dataForm" ref="dataForm" inline size="small" onsubmit="return false">
        <el-row>
          <el-form-item>
            <el-input placeholder="搜索课程标题关键词" v-model="dataForm.freeCondition" maxlength="30" clearable />
          </el-form-item>
          <el-form-item label-width="80px">
            <el-select placeholder="发布人员" v-model="dataForm.publisher" clearable>
              <el-option v-for="item in users" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-date-picker placeholder="发布时间" type="date" value-format="yyyy-MM-dd" v-model="dataForm.createTime" style="width:220px" />
          </el-form-item>
          <el-form-item>
            <el-select placeholder="选择课程类型" v-model="dataForm.courseType" clearable @change="dataForm.secondCourseType = ''">
              <el-option v-for="(item,key) of course" :key="key" :label="item" :value="key" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select placeholder="选择二级类型" v-model="dataForm.secondCourseType" :disabled="dataForm.courseType === ''" clearable>
              <el-option v-for="(item,key) of dataForm.courseType.children || []" :key="key" :label="item" :value="key" />
            </el-select>
          </el-form-item>
        </el-row>
        <el-row style="margin:20px 0 0 0px;">
          <el-form-item>
            <el-button type="primary" @click="getList(1)">
              <fa-icon class="fa-search" />查询</el-button>
            <reset-button :dataForm.sync="dataForm" :fixedData="{pageNum: 1, pageSize: 10}" />
            <el-button type="primary" @click="batchCancel">
              批量下载</el-button>
            <el-button type="primary" @click="batchAssign">
              批量删除</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="top-panel__box">
      <el-button type="primary" @click="batchCancel">
        发布课程</el-button>
      <el-button @click="batchCancel">
        分类管理</el-button>
    </div>
    <el-row class="table-top">
      <el-table :data="dataList" v-loading="tableLoading" tooltip-effect="light" @selection-change="handleSelectionChange" class="tc-table" size="small">
        <el-table-column type="selection" width="55" fixed="left"></el-table-column>
        <template v-for="(column, index) in REPAYMENT_HEAD_LIST">
          <el-table-column :prop="column.prop" :label="column.label" :key="index" :width="column.width" :show-overflow-tooltip="true" />
        </template>
        <el-table-column prop="operation" class-name="oper-col" label="操作" :width="180">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" plain @click="detailRow(scope.row)">详情</el-button>
            <el-button size="mini" type="primary" plain v-if="scope.row.currentStatus==='1'" @click="cancelRow(scope.row)">撤回</el-button>
            <el-button size="mini" type="primary" plain v-if="scope.row.currentStatus==='2'" @click="assignRow(scope.row)">分派</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row class="pagination-top">
        <pagination :pageNum.sync="dataForm.pageNum" :curSizes.sync="dataForm.pageSize" :total="total" @getList="getList" />
      </el-row>
    </el-row>
    <!--分派-->
    <el-dialog title="分派" :visible.sync="assignDialog" width="40%" :close-on-click-modal="false">
      <assign-acceptor v-if="assignDialog" @uppdataSuccess="uppdataSuccess" />
    </el-dialog>
    <!--详情-->
    <el-dialog :title="editData.customerName" :visible.sync="detailDialog" width="60%" :close-on-click-modal="false">
      <repayment-detail v-if="detailDialog" :overdueId="editData.overdueId" />
    </el-dialog>
  </div>
</template>

<script>
import { CURRENT_STATUS, REPAYMENT_HEAD_LIST } from './js/contant.js'
import AssignAcceptor from '@/components/assignAcceptor/AssignAcceptor'
import RepaymentDetail from './components/RepaymentDetail'
import api from './api'
import { CONSTANT_OBJ } from '@/utils/constant'
import { common } from '@/utils/common'
import { mapState } from 'vuex'
export default {
  components: { AssignAcceptor, RepaymentDetail },
  data () {
    return {
      CURRENT_STATUS,
      REPAYMENT_HEAD_LIST,
      productNoList: [],
      tableLoading: false,
      assignDialog: false,
      detailDialog: false,
      editData: {},
      dataForm: {
        publisher: '',
        createTime: '',
        courseType: '',
        secondCourseType: '',
        freeCondition: '',
        acceptorName: '',
        currentStatus: '',
        minOverdueDays: '',
        maxOverdueDays: '',
        pageNum: 1,
        pageSize: 10
      },
      publisher: '',
      total: 0,
      dataList: [],
      slectedList: [],
      assignOverdueIds: '',
      selectedIds: [],
      users: [],
      course: []
    }
  },
  mounted () {
    this.$store.dispatch('common/queryProductData', { pageNum: 1, pageSize: 99 })
    this.getUsers()
    this.getList()
  },
  methods: {
    // 获取人员列表
    getUsers () {
      this.users = [
        {
          id: '1',
          name: '张三'
        },
        {
          id: '2',
          name: '李四'
        }
      ]
    },
    // 获取课程分类树
    getCourse () {
      this.course = [
        {
          id: '1',
          name: '业务类培训',
          children: [
            {
              id: '3',
              name: '零售金融类'
            }
          ]
        },
        {
          id: '2',
          name: '技能类培训',
          children: [
            {
              id: '4',
              name: '获客类'
            }
          ]
        }
      ]
    },
    // 查询列表
    getList (val) {
      this.dataForm.pageNum = val || this.dataForm.pageNum
      this.tableLoading = true
      api.postQueryOverdueData(this.dataForm).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.dataList = data.list || []
          this.dataList.forEach(item => {
            this.$set(item, 'currentStatusName', CURRENT_STATUS[item.currentStatus] || item.currentStatus)
          })
          this.total = parseInt(data.total) || 0
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 选中
    handleSelectionChange (val) {
      this.slectedList = val
      this.selectedIds = []
      this.slectedList.forEach(item => {
        this.selectedIds.push(item.overdueId)
      })
    },
    // 批量撤回
    batchCancel () {
      if (!this.checkoutData('1')) {
        return false
      }
      this.cancelRow({ overdueId: this.selectedIds.join(',') })
    },
    // 撤回
    cancelRow ({ overdueId }) {
      let overdueIds = overdueId
      common.confirmFn('撤回后您可重新进行分派', '是否确定撤回此分派').then(res => {
        if (res) {
          api.postRepayCancelData(overdueIds).then(res => {
            this.successFn(res)
          })
        }
      })
    },
    // 分派
    assignRow ({ overdueId }) {
      this.assignDialog = true
      this.assignOverdueIds = overdueId
    },
    // 批量分派
    batchAssign () {
      if (!this.checkoutData('2')) {
        return false
      }
      this.assignRow(this.selectedIds.join(','))
    },
    // 校验数据
    checkoutData (val) {
      let result = true
      let data = [...this.slectedList]
      if (data.length === 0) {
        this.$message({
          message: '请选择待撤回案件',
          type: 'warning'
        })
        result = false
      }
      let allData = data.every((item) => (item.currentStatus === val))
      if (!allData) {
        this.$message({
          message: '请正确选择待撤回案件',
          type: 'warning'
        })
        result = false
      }
      return result
    },
    uppdataSuccess (acceptorId) {
      let data = {
        acceptorId,
        overdueIds: this.assignOverdueIds
      }
      api.postAssignAcceptorsData(data).then(res => {
        this.successFn(res, 'assignDialog')
      })
    },
    successFn (res, dialog) {
      const { responseCode, responseMsg: message } = res
      if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
        this.$message({
          message,
          type: 'success'
        })
        if (dialog) {
          this[dialog] = false
        }
        this.getList(1)
      }
    },
    // 获取详情
    detailRow (row) {
      this.editData = { ...row }
      this.detailDialog = true
    }
  },
  computed: {
    ...mapState({
      products: state => state.common.products
    })
  }
}
</script>
